<?php

class HistoryController extends CController
{
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionOrder() {
		//当客户下订单后：
		//(1)判定此客户是否来自于Affiliate
		//(2)记录每一笔订单的详细信息
		//(3)计算网站主每天有多少订单
		
		$data = $_GET;
		$trans = Yii::app()->db->beginTransaction();
		try {
			//计算此会员产生了多少次订单，订单总额是多少
			$params = array(':user_id' => $data['user_id']);
			$user = User::model()->find('user_id=:user_id', $params);
			if (!$user) {//此会员不属于 Affiliate，订单不作处理
				return ;
			}
			$user->ordernum ++;
			$user->order_amount += $data['amount'];
			$user->save();
	
			$website = Website::model()->find('domain=:website', array(':website' => $data['website']));
			$website->ordernum ++;
			$website->order_amount += $data['amount'];
			$website->save();
			
			//记录此次订单的基本信息
			$order_history = new OrderHistory();
			$order_history->attributes = array(
				'order_id' => $data['order_id'],
				'user_id' => $data['user_id'],
				'owner_id' => $user->owner_id,
				'website' => $data['website'],
				'website_id' => $website->website_id,
				'adv_id' => $user->adv_id,
				'amount' => $data['amount'],
				'username' => $data['username'],
				'note' => $data['note'],
				'adddate' => date("Y-m-d"),
			);
			$order_history->save();
			
			//计算网站主一共带来了多少个订单，带来多少订单金额 
			$owner = Owner::model()->find('loginid=:loginid', array(':loginid' => $user->owner_id));
			$owner->ordernum ++;
			$owner->order_amount += $data['amount'];
			$owner->save();
			
			//计算广告一共带来了多少个订单，带来多少订单金额
			$adv = Adv::model()->findByPk($user->adv_id);
			$adv->ordernum ++;
			$adv->order_amount += $data['amount'];
			$adv->save();
			
			$diary_log = DiaryLog::load($user->owner_id, $website->website_id);
			
			//计算是否为 First Order
			//First Order 规则：此会员注册后的 X 天内下的第一张订单
			if (time() - strtotime($user->date_join) <= Yii::app()->params['firstOrderIndate']*24*3600) {
				$count = OrderHistory::model()->count('user_id=:user_id', array(':user_id' => $user->user_id));
				if ($count == 1) {//$count == 1 意味着这是第一次下的订单
					$first_order = new FirstOrder();
					$first_order->order_id = $data['order_id'];
					$first_order->save();
					
					$diary_log->firstordernum ++;
					$diary_log->firstorderamount += $data['amount'];
				}
			}
			$diary_log->ordernum ++;
			$diary_log->orderamount += $data['amount'];
			$diary_log->save();
			
			$trans->commit();
		} catch (Exception $e) {
			$trans->rollback();
		}
	}
	
	/**
	 * 记录访客的注册
	 * @param int $uid
	 * @param int $oid
	 */
	public function actionRegister($uid) {
		//来自推广的访客,如果该IP重复注册，则只记录一次
		// TODO 这里到时要去掉 “1”
		if (1 || sess('owner_id') && !sess('registered')) {
			$trans = Yii::app()->db->beginTransaction();
			try {			
				/**
				 * 访客点击广告过来的时候，session 会记录他的 owner_id, adv_id
				 * 他注册后，也要记录他的 owner_id, adv_id
				 */
				$user = new User();
				$user->attributes = array(
					'user_id' => $uid,
					'owner_id' => sess('owner_id'),
					'adv_id' => sess('adv_id'),
					'website_id' => sess('website_id'),
					'website' => Website::model()->findByPk(sess('website_id'))->domain,
					'adv_click_history_id' => sess('adv_click_history_id'),
					'referer' => sess('referer'),
					'accept' => 'P',
					'date_join' => date('Y-m-d H:i:s'),
					'ip_active' => '0',
					'client_browser' => $_SERVER['HTTP_USER_AGENT']
				);
				$user->save();
				
				$adv = Adv::model()->findByPk(sess('adv_id'));
				$adv->joinmb ++;
				$adv->save();
				
				$website = Website::model()->findByPk(sess('website_id'));
				$website->joinmb ++;
				$website->save();
				
				$diary_log = DiaryLog::load(sess('owner_id'), sess('website_id'));
				$diary_log->joinmb ++;
				$diary_log->save();
				
				$owner = Owner::model()->find('loginid=:loginid', array(':loginid' => sess('owner_id')));
				$owner->joinmb ++;
				$owner->save();
				
				$trans->commit();
				
				//标记当前会话中，来自此 owner_id 的访客已经注册，以后此会话中的注册就不重复记录
				sess('registered', sess('owner_id')); 	
			} catch (Exception $e) {
				$trans->rollback();
			}	
		}
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}